// require base class
Flair.require('Flair/Db/Adapter.js');

/**
 * Class for performing common operations on an sqlConnection synchronously
 */
Flair.Db.Sync = Flair.extend(Flair.Db.Adapter, function () {
	
	/**
	 * Run an sql query.
	 * Optionally run asynch by passing a responder 
	 *
	 * @param string sql
	 * @param array params
	 * @param Responder resp
	 */
	this.query = function (sql, params) { 
		var statement = new air.SQLStatement();
		statement.sqlConnection = this.connection;
		
		statement.text = sql;
		Flair.log('connection:',this.connection);
		if(params)
			for (var i in params)
				statement.parameters[i] = params[i];
		
		statement.execute();
			
		return statement;
	};

	/**
	 * Run sql query and fetch all returned records
	 *
	 * @param string sql
	 * @param array params
	 */
	this.fetchAll = function (sql, params) { 
		var stmt = this.query( sql, params );
		return this._fetchAll( stmt.getResult() );
	};
	
	this.fetchRow = function (sql, params) { 
		var stmt = this.query( sql, params );
		return this._fetchRow( stmt.getResult() );
	};
	this.fetchCol = function (sql, params) { 
		var stmt = this.query( sql, params );
		return this._fetchCol( stmt.getResult() );
	};
	this.fetchOne  = function (sql, params) { 
		var stmt = this.query( sql, params );
		return this._fetchOne( stmt.getResult() );
	};

});